const express = require('express')
const router = express.Router()
const jwt = require('jsonwebtoken')
const settings = require('../../config/settings')

router.use((req, res, next) => {
    let {
        url = ''
    } = req
    const login = url.indexOf('/api/users/login')
    const screen = url.indexOf('/api/screen')
    const captcha = url.indexOf('/api/users/captcha')
    if (!login) { // 除去登陆页的请求
        return next()
    }
    if (!screen) { // 除去电视屏幕页的请求
        return next()
    }
    if (!captcha) { // 除去登录验证码的请求
        return next()
    }

    jwt.verify(
        req.headers['token'],
        settings.token_secret_key,
        (err) => {
            if (err) {
                console.log(err)
                return res.json({
                    status: 401,
                    msg: 'no auth'
                })
            }
            return next() //继续下一步路由
        })
})

module.exports = router